const commonUser = require('../../../common/commonUser.js');
const pageObject = {
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
  },
};

Page({
  pageObject,
  data: {
    parent_id: 0,
    admin_id: 0,
    redirce_url: '/pages/users/index/index',
    peris_id:0,
    tobbarUrl:[
      '/pages/index/index',
      '/pages/peris/index/index',
      '/pages/messages/index/index',
      '/pages/users/index/index'
    ]
  },
  onLoad: function (options) {
    let that = this;
    //获取通过分享链接进入的用户上级id
    let parent_id = wx.getStorageSync('share_parent_id');
    let admin_id = wx.getStorageSync('share_admin_id');
    if (parent_id != undefined && parent_id != null && parent_id > 0) {
      that.data.parent_id = parent_id;
      console.log(parent_id);
    }
    if (admin_id != undefined && admin_id != null && admin_id > 0) {
      that.data.parenadmin_idt_id = admin_id;
      console.log(parent_id);
    }
    //获取上一个页面
    if (options.url) {
      let url = options.url;
      if(options.peris_id){
        url = url+'?id='+options.peris_id;
      }
      that.setData({
        redirce_url: url
      })
    } else {
      that.data.redirce_url = '/pages/users/index/index';
    }
  },
  onShow: function () {
    
  },

  //微信一键登录
  wechatLogin: function (re) {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {
          commonUser.userLogin(res.code, '', '', {
            parent_id: that.data.parent_id,
            admin_id: that.data.admin_id
          }).then(function (re) {
            if (re) {
              //清除缓存
              wx.setStorageSync('share_parent_id','');
              wx.setStorageSync('share_admin_id','');
              wx.showToast({
                title: '登录成功',
                success:function() {
                  //判断当前路径
                  let isTobbar = false;
                  for(let i in that.data.tobbarUrl){
                    if(that.data.redirce_url == that.data.tobbarUrl[i]){
                      isTobbar = true;
                      break;
                    }
                  }
                  if(isTobbar){
                    wx.switchTab({
                      url: that.data.redirce_url,
                    })
                  }else{
                    wx.reLaunch({
                      url: that.data.redirce_url,
                    });
                  }
                }
              })
            }else{
              wx.showToast({
                title: '登录失败',
              })
            } 
          })
        }
      }
    })
  },  
})